<template>
  <div class="zy-index" v-if="model">
    <image class="top" mode="widthFix" src="http://static.myconplus.com/20190621/5d0c747f9f6fe"/>
    <div class="top-section">
      <swiper v-if="model.banners"  class="swiper" height="100%" indicator-dots="true" autoplay="true" interval="5000" duration="1000">
        <swiper-item v-for="(item, index) in model.banners" :index="index" :key="index">
          <image :src="item.image" class="slide-image" mode="widthFix" @click="handleRouter(item)"/>
        </swiper-item>
      </swiper>
      <image class="red-packet" mode="widthFix" src="http://static.myconplus.com/20190621/5d0c78c851e39" @click="onGetNewRedPacketA"/>
      <div class="nav-tips" v-if="model">
        <div class="nav">
        <img  src="http://static.myconplus.com/20190609/5cfc7ec19584c"/>
        <img  src="http://static.myconplus.com/20190609/5cfc7ec175868"/>
        <img  src="http://static.myconplus.com/20190609/5cfc7ec19163e"/>
        <img  src="http://static.myconplus.com/20190609/5cfc7ec17b30d"/>
        </div>
        <div  class="head">
         <div class="nav-menu" v-for="(item, index) in model.nav_menus" :key="index" @click="showSonNAv(item)">
            <img :src="item.image" >
            <span>{{item.display_name}}</span>
         </div>
       </div>
       <div class="son-nav">
          <img class="item" v-for="(item, index) in sonNavs" :key="index" :src="item.image"  @click="handleRouter(item)">
       </div>
      </div>
    </div>
    <swiper v-if="model.daily_plate&&model.daily_plate.length"  class="card-swiper" autoplay="true" interval="5000" previous-margin="45px" next-margin="30px" circular duration="1000">
      <swiper-item v-for="(item, index) in model.daily_plate" :index="index" :key="index">
        <image :src="item.image" class="slide-image" mode="widthFix" @click="handleRouter(item)"/>
      </swiper-item>
    </swiper>
    <div class="mc-title">
      <div class="border"></div>
      <span class="title">好物推荐</span>
      <span class="info">你想要的都在这里</span>
    </div>
    <div class="data-section" v-if="model.DataItems&&model.DataItems.length">
      <div v-for="(item, index) in model.DataItems" :key="index" @click="handleRouter(item)">
        <image class="section" :src="item.image" mode="widthFix"  />
        <div class="title">{{item.title}}</div>
      </div>
    </div>
    <div class="post-section" v-if="publicPosts&&publicPosts.length">
      <div v-for="(item, index) in publicPosts" :key="index" @click="toPublicPost(item)" >
        <image class="section" :src="item.cover"  mode="widthFix"/>
        <div class="title">{{item.title}}</div>
      </div>
    </div>
    <block  v-if="brandsList&&brandsList.length">
      <div class="brand-section" v-for="(item, index) in brandsList" :key="index">
        <image class="background" mode="widthFix" :src="item.cover + '?imageMogr2/auto-orient/thumbnail/640x/quality/75'"/>
        <div class="content" @click="toBrand(item.brand_id)">
          <image class="logo" :src="item.logo + '?imageView2/1/w/120/h/120/q/75'"/>
          <div class="dec">
            <div class="title">
              <span>{{item.name}}</span>
              <span class="btn">
                查看全部 
                <van-icon name="arrow"  style="top: 2rpx; display: inline-block; position:relative;"/>
              </span>
            </div>
            <div class="intro">{{item.introduction}}</div>
          </div>
        </div>
        <div class="brand-products">
        <van-row>
          <van-col
            span="8"
            v-for="(sitem, idx) in item.products"
            :key="idx"
            @click="toProductDetail(sitem.id)"
          >
            <div class="card">
              <image mode="aspectFill" :src="sitem.image + '?imageView2/1/w/320/h/320/q/75'" lazy-load="true"/>
              <div class="card-info">
                <p class="head">{{sitem.title}}</p>
                <p class="mk-price">￥{{sitem.price}}</p>
              </div>
            </div>
          </van-col>
        </van-row>
      </div>
    </div>
    </block>
    <van-toast id="van-toast"/>
    <van-dialog id="van-dialog"/>

  </div>
</template>
<script>
import * as OperationAction from '@/api/operation'
import * as AxitivityAction from '@/api/acitvity-616'
import * as PostAction from '@/api/post'
import * as ZyIndexAction from '@/api/zy-index'
import Dialog from 'vant-weapp/dist/dialog/dialog'
import { mapState } from 'vuex'
import Utils from '@/utils'

import Toast from 'vant-weapp/dist/toast/toast'

export default {
  data () {
    return {
      model: null,
      publicPosts: null,
      sonNavs: [],
      brandsList: null
    }
  },
  computed: mapState(['me']),
  onLoad (query) {
    wx.showNavigationBarLoading()
    this.fetchIndex()
    this.fetchBrandList()
    this.fetchPublicPosts()
    if (query.scene) {
      let params = Utils.parseQuery(decodeURIComponent(query.scene))
      if (params.ic && params.ic.length === 6) wx.setStorageSync('__inviteCode', params.ic)
      return
    }
    if (query.code) wx.setStorageSync('__inviteCode', query.code)
  },
  onPullDownRefresh () {
    wx.showNavigationBarLoading()
    this.fetchIndex()
    this.fetchBrandList()
    this.fetchPublicPosts()
    wx.stopPullDownRefresh()
  },
  onShareAppMessage (res) {
    return {
      title: this.shareTitle(),
      path: this.sharePath()
    }
  },

  methods: {
    toProductDetail (id) {
      wx.navigateTo({ url: '../product/detail?id=' + id })
    },
    toBrand (id) {
      wx.navigateTo({ url: '../brand/index?id=' + id })
    },
    shareTitle () {
      if (!this.me) return `美控自营`
      if (this.me) return `${this.me.nickname}向您推荐了美控自营`
    },
    sharePath () {
      let url = '/pages/home/zy?'
      let code = ''
      if (this.me && this.me.invite_code) {
        code = this.me.invite_code
        url = url + '&code=' + code
      }
      return url
    },
    onGetNewRedPacketA () {
      if (!this.me) {
        this.loginGuide()
        return
      }
      AxitivityAction.getRedPacketA().then(data => {
        Toast(data.msg || '抢到优惠券啦')
      }).catch(err => {
        if (err.code === 500) {
          Toast('请求出错，请重试！')
        } else {
          Toast(err.msg)
        }
      })
    },
    toPublicPost (item) {
      wx.navigateTo({ url: '../public-post/detail?id=' + item.id })
    },
    loginGuide () {
      Dialog.confirm({ message: '请登录后领取该红包，是否前往登录？' })
        .then(() => {
          wx.redirectTo({ url: '../auth/login-index?target=zy' })
        }).catch(() => { })
    },
    handleRouter (item) {
      switch (item.open_type) {
        case 'goods-zone':
          wx.navigateTo({
            url: '../product/zone?id=' + item.target
          })
          break
        case 'placardList':
          wx.navigateTo({
            url: '../placard/list'
          })
          break
        case 'placardDetail':
          wx.navigateTo({
            url: '../placard/detail?id=' + item.target
          })
          break
        case 'goods':
          wx.navigateTo({
            url: '../product/detail?id=' + item.target
          })
          break
        case 'goods-search':
          wx.navigateTo({
            url: '../product/search?title=' + item.target
          })
          break
        case 'brand':
          wx.navigateTo({
            url: '../brand/index?id=' + item.target
          })
          break
        case 'category':
          wx.navigateTo({
            url: '../brand/category'
          })
          break
      }
    },
    showSonNAv (item) {
      this.sonNavs = item.brands
    },
    // 获取品牌相关信息List
    fetchBrandList () {
      OperationAction.fetchBrands({ is_proprietary: 1 })
        .then(data => {
          this.brandsList = data
        }).catch(err => console.log(err.msg))
    },
    fetchIndex () {
      ZyIndexAction.fetchIndex({ p: 'mp' })
        .then(data => {
          this.model = data
          this.sonNavs = data.nav_menus[0].brands
          wx.hideNavigationBarLoading()
        })
        .catch(err => Toast(err.msg))
    },
    fetchPublicPosts () {
      PostAction.publicPosts().then(data => {
        this.publicPosts = data.data
      }).catch(err => Toast(err.msg))
    }
  }
}
</script>
<style lang="less" scoped>
@theme-red: #ED6D96;
  .mk-price {
    color: @theme-red;
    margin: 16rpx 0;
    font-size: 30rpx;
    text-align: left;
    font-weight: 400;
    height: 40rpx;
    overflow: hidden;
  }
.zy-index {
  position: relative;
  background: #FEFFFF;
  width: 100%;
  .top {
    width: 100%;
  }
  .card-swiper {
    width: 100%; 
    margin-top: 40rpx;
    height: 600rpx;
    border-radius: 10px;
    position: relative;
    .slide-image {
      width: 95%;
      border-radius: 20rpx;
    }
  }
  .mc-title {
    margin: 30rpx 30rpx 40rpx;
    padding: 0 20rpx 0 0;
    .border {
      background-color: #FD5D99;
      width: 8rpx;
      height: 32rpx;
      margin-right: 15rpx;
      display: inline-block;
      position: relative;
      top: 3rpx;
    }
    .title {
      font-size: 35rpx;
      font-weight: 500;
      color: #19191A;
    }
    .info {
      font-size: 22rpx;
      color: #868788;
      font-weight: 400;
      margin-left: 20rpx;
    }
  }
  .top-section {
    position: absolute;
    top: 25rpx;
    left: 0;
    right: 0;
    margin: 0 20rpx;
    width: 95%;
    .swiper {
      height: 324rpx;
      margin-bottom: 20rpx;
      .slide-image {
        height: 325rpx;
        width: 100%;
        border-radius: 20rpx;
      }
  }
  .red-packet {
    width: 100%;
  }
  .nav-tips {
    width: 100%;
    background: #fff;
    font-size: 15px;
    box-sizing: border-box;
    border-radius: 30rpx;
    padding: 25rpx;
    margin-top: 10rpx;
    .nav {
      width: 100%;
      display: flex;
      justify-content: space-between;
      margin-bottom: 20rpx;
      img {
        width: 113rpx;
        height: 30rpx;
      }
    }
    .head {
      width: 100%;
      display: flex;
      justify-content: space-between;
      margin-bottom: 20rpx;
          .nav-menu {
        img {
          width:80rpx;
          height: 80rpx;
          margin-bottom: 0;
        }
        span {
          display: block;
          color: #939393;
          text-align: center;
          font-size: 24rpx;
        }
      }
      .nav-menu:hover {
        span {
        color: #F96796;
        }
      }
    }
    .son-nav {
      background: #F4F5F6;
      color: #3D3D3F;
      border-radius: 6px;
      border: 1px solid #EFF0F1;
      padding: 8rpx 20rpx;
      .item {
        height: 55rpx;
        width: 190rpx;
        margin: 8rpx 8rpx 0;
      }
    }
  }
  }
   .data-section {
    padding: 0 16px;
    margin-top: -28rpx;
    .title {
       color: #333333;
        font-size: 30rpx;
        padding: 10rpx 10rpx 30rpx;
        }
        .section {
          width: 100%;
          border-radius: 16rpx;
        }
      }
      .post-section {
        padding:0 16px;
          .title {
            color: #333333;
            font-size: 30rpx;
            padding: 10rpx 10rpx 30rpx;
            }
    .section {
      width: 100%;
      border-radius: 16rpx;
    }
  }
}
.red-card {
  width: 300px;
  height: 307px;
  background-size: cover;
  text-align: center;
  box-sizing: border-box;
  padding: 17px;
  span {
    color: #b36321;
    font-weight: 500;
    font-size: 20px;
  }
}
  .card {
    text-align: center;
    border-radius: 8rpx;
    width: 200rpx;
    margin-left: 8rpx;
    image {
      width: 200rpx;
      height: 200rpx;
      border-radius: 8rpx 8rpx 0 0;
    }
    .card-info {
      box-sizing: border-box;
      position: relative;
      background: #fff;
      top: -11rpx;
      padding: 6rpx;
      border-radius: 0 0 8rpx 8rpx;
      .mk-price {
        margin: 0;
      }
      .head {
        font-size: 22rpx;
        height: 65rpx;
        text-align: left;
        text-justify: kashida;
        overflow: hidden;
      }
    }
  }
  .brand-section {
    position: relative;
    padding: 0;
    margin: 15rpx 30rpx;
    .content {
      position: absolute;
      z-index: 55;
      top: 0;
      display: flex;
      flex-flow: row wrap;
      width: 100%;
      .logo {
        width: 110rpx;
        height: 110rpx;
        border-radius: 50%;
        margin: 40rpx 20rpx;
      }

      .dec {
        flex: 1;
        color: #fff;
        padding: 40rpx 8rpx;
        position: relative;
        display: flex;
        flex-flow: column;
        justify-content: space-around;

        .title {
          color: #fff;
          display: flex;
          justify-content: space-between;

          .btn {
            background: #fff;
            color: #333;
            padding: 0rpx 20rpx;
            margin-right: 35rpx;
            font-weight: 400;
            font-size: 18rpx;
            border-radius: 36rpx;
            height: 40rpx;
            line-height: 40rpx;
            margin-top: 4rpx;
          }
        }

        .intro {
          font-size: 23rpx;
          min-width: 524rpx;
          height: 38px;
          box-sizing: border-box;
          overflow: hidden;
        }
      }
    }
    .brand-products {
      position: absolute;
      z-index: 55;
      top: 160rpx;
      left:0;
      right: 0;
      padding: 20rpx;
      overflow: hidden;
    }
    .background {
      width: 100%;
      border-radius: 10rpx;
    }
  }
</style>
